package nl.wubinator.aeve.xmlconverters.standard;

import nl.wubinator.aeve.models.standard.alliancelist.Alliance;
import nl.wubinator.aeve.models.standard.alliancelist.AllianceList;
import nl.wubinator.aeve.models.standard.alliancelist.Corporation;
import nl.wubinator.aeve.xmlconverters.ConverterBase;
import nl.wubinator.aeve.xmlconverters.standard.parsers.AllianceParser;
import nl.wubinator.aeve.xmlconverters.standard.parsers.CorporationParser;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.util.Log;


public class AllianceListConverter extends ConverterBase {
	// Used to define what elements we are currently in
	private boolean inCurrentTime = false;
	private boolean inError = false;
	private boolean inCachedUntil = false;
	private boolean inAlliancesRowset = false;
	private boolean inMemberCorporationsRowSet = false;

	// Character object to use for temporary storage
	private Alliance currentAlliance;
	private Corporation currentCorporation;

	// The complete characters object that will be filled in this converter
	private AllianceList allianceList = new AllianceList();

	public void startElement(String uri, String name, String qName,
			Attributes atts) {
		if (name.trim().equals("evapi"))
			allianceList.setVersion(Integer.parseInt(atts.getValue("version")));
		else if (name.trim().equals("error")) {
			allianceList.setErrorCode(Integer.parseInt(atts.getValue("code")));
			inError = true;
		}
		if (name.trim().equals("currentTime"))
			inCurrentTime = true;
		else if (name.trim().equals("cachedUntil"))
			inCachedUntil = true;
		else if (name.trim().equals("rowset")) {
			String rowsetAttributes = atts.getValue("name");
			if (rowsetAttributes.equals("alliances")) {
				inAlliancesRowset = true;
			}
			if (rowsetAttributes.equals("memberCorporations")) {
				inMemberCorporationsRowSet = true;
			}
		} else if (name.trim().equals("row")) {
			if (inAlliancesRowset && !inMemberCorporationsRowSet) {
				this.currentAlliance = AllianceParser.parseAttributes(atts);
			} else if (inMemberCorporationsRowSet) {
				this.currentCorporation = CorporationParser.parseAttributes(atts);
			}
		}
	}

	public void endElement(String uri, String name, String qName)
			throws SAXException {
		if (name.trim().equals("error"))
			inError = false;
		if (name.trim().equals("currentTime"))
			inCurrentTime = false;
		else if (name.trim().equals("cachedUntil"))
			inCachedUntil = false;
		else if (name.trim().equals("rowset")) {
			inAlliancesRowset = false;
			inMemberCorporationsRowSet = false;
		} else if (name.trim().equals("row")) {
			if (inAlliancesRowset && !inMemberCorporationsRowSet)
				this.allianceList.addAlliance(this.currentAlliance);
			else if (inMemberCorporationsRowSet)
				this.currentAlliance.addCorporation(this.currentCorporation);
		} else if (name.trim().equals("eveapi"))
			setResult(allianceList);
	}

	public void characters(char ch[], int start, int length) {
		String chars = (new String(ch).substring(start, start + length));
		try {
			if (inError)
				this.allianceList.setError(chars);
			if (inCurrentTime)
				this.allianceList.setCurrentTime(chars);
			else if (inCachedUntil)
				this.allianceList.setCachedUntil(chars);
		} catch (Exception e) {
			Log.e("Reading XML error", e.getMessage());
		}
	}
}
